﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
using System.Text.RegularExpressions;

namespace HtmlToText
{
    public class HtmltoTextConverter
    {
        /// <summary>
        /// Remove HTML from string with Regex.
        /// </summary>
        public static string StripTagsRegex(string source)
        {
            var convertedText = Regex.Replace(source, "<.*?>", string.Empty);
            convertedText = Regex.Replace(convertedText, @"&nbsp", " ");
            return FormatText(convertedText);
        }

        /// <summary>
        /// Remove HTML tags from string using char array.
        /// </summary>
        private static string FormatText(string source)
        {
            var over = new Regex(@"[0-9]*\.[0-6]\b");
            var words = source.Split(',', '\n', '\t', ';', ' ', '\r');
            var check = false;
            string finalText = string.Empty;
            var count = words.Length;
            for(int i = 0; i< count; i++)
            {
                ////if (words[i].CompareTo("Player") == 0 && words[i + 1].CompareTo("v") == 0)
                if(over.IsMatch(words[i]))
                {
                    check = true;
                }
                if(check == true)
                {
                    if (over.IsMatch(words[i]) && (string.CompareOrdinal(words[i+1], "mph")!=0 && string.Compare(words[i+1],"kmph", StringComparison.OrdinalIgnoreCase)!= 0 && string.Compare(words[i+1],"kmh", StringComparison.OrdinalIgnoreCase)!= 0 && string.Compare(words[i+1], "kph", StringComparison.OrdinalIgnoreCase)!= 0))
                    {
                        finalText = finalText + "\n";
                    }
                    finalText = finalText + words[i] + " ";
                    
                }
            }
            return finalText;
        }
    }
}

